<template>
  <div class="base-table">
    <el-inline-form
      :model="searchParams"
      label-size="default"
      class="form-bottom-border"
      @do-search="handleSearch"
    >
      <el-inline-form-item
        prop="ruleName"
        label="项目名称"
      >
        <el-input v-model="searchParams.ruleName" />
      </el-inline-form-item>
      <el-inline-form-item
        prop="describe"
        label="描述"
      >
        <el-input v-model="searchParams.describe" />
      </el-inline-form-item>
      <el-inline-form-item
        prop="name"
        label="公司名称"
      >
        <el-input v-model="searchParams.name" />
      </el-inline-form-item>
      <el-inline-form-item
        prop="categoryType"
        label="合同类别"
      >
        <el-input v-model="searchParams.categoryType" />
      </el-inline-form-item>
      <el-inline-form-item
        prop="time"
        label="日期"
      >
        <el-date-picker
          v-model="searchParams.time"
          type="date"
          format="YYYY-MM-DD"
          placeholder="请选择日期"
        />
      </el-inline-form-item>
      <el-inline-form-item
        prop="time"
        label="日期一"
      >
        <el-date-picker
          v-model="searchParams.time"
          type="date"
          format="YYYY-MM-DD"
          placeholder="请选择日期"
        />
      </el-inline-form-item>
      <el-inline-form-item
        prop="time"
        label="日期二"
      >
        <el-date-picker
          v-model="searchParams.time"
          type="date"
          format="YYYY-MM-DD"
          placeholder="请选择日期"
        />
      </el-inline-form-item>
    </el-inline-form>

    <el-operate-block class="mt-12 mb-12">
      <template #title>
        <span>基础表格</span>
        <span class="select-number">已选: <span class="num">{{ selectedItems }}</span>条</span>
      </template>
      <template #actions>
        <el-button @click="handleExport([tableData[1], tableData[2]])">
          导出
        </el-button>
        <el-button @click="handleOperation()">
          业务操作
        </el-button>
        <el-button @click="handleBatchOperation()">
          批量操作
        </el-button>
        <el-button @click="handleAdd()">
          新增
        </el-button>
      </template>
    </el-operate-block>

    <el-table
      ref="multipleTableRef"
      :data="tableData"
      style="width: 100%;"
      empty-text="暂无数据"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        property="name"
        label="姓名"
        fixed="left"
        :min-width="100"
      />
      <el-table-column
        property="regionProject"
        label="地区项目"
        :min-width="120"
      />
      <el-table-column
        property="quotedPrice"
        label="报价(万元)"
        sortable
        :min-width="110"
      />
      <el-table-column
        property="subscriptionRatio"
        label="认购达成(万元)"
        sortable
        :min-width="130"
      />
      <el-table-column
        property="effectiveDate"
        label="有效日期"
        :min-width="120"
      >
        <template
          #default="scope"
        >
          {{ scope.row.effectiveDate }}
        </template>
      </el-table-column>
      <el-table-column
        property="img"
        label="图片"
        :min-width="60"
      >
        <template
          #default="{ row }"
        >
          <div class="picture">
            <img
              :src="row.img"
              height="40"
              alt=""
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column
        property="processState"
        label="处理状态"
        :min-width="90"
      >
        <template
          #default="{ row }"
        >
          <div>
            <span
              class="status-dot"
              :style="{ background: getProcessStateColor(row.processState) }"
            />
            {{ getProcessStateText( row.processState ) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        property="processState"
        label="启用状态"
        :min-width="90"
      >
        <template
          #default="{ row }"
        >
          <el-switch
            v-model="row.initiateMode"
            inline-prompt
            active-text="启用"
            inactive-text="禁用"
          />
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="120"
      >
        <template #default>
          <el-button
            link
            type="primary"
            size="small"
            @click="handleEdit"
          >
            操作
          </el-button>
          <el-button
            link
            type="primary"
            size="small"
            @click="handleDelete"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <base-pagination
      :total="total"
      @do-change="pageChangeHandle"
    />
  </div>
</template>
<script lang="ts" setup name='base-table'>
import {
  ref,
  onMounted,
} from 'vue';

import { ElMessage } from 'hive-plus';

interface TableDataItem {
  name: string
  address: string
  regionProject: string
  subscriptionRatio: number
  effectiveDate: string
  img: string
  initiateMode: boolean
  processState: number
  quotedPrice: number
  index: number
}

const tableDataTemp:TableDataItem[] = [
  {
    name: 'Tom',
    quotedPrice: 293,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 2344,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: true,
    processState: 1,
    index: 1,
  },
  {
    name: 'Tom',
    quotedPrice: 23,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 234,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: true,
    processState: 2,
    index: 2,
  },
  {
    name: 'Tom',
    quotedPrice: 63,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 4,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: true,
    processState: 3,
    index: 3,
  },
  {
    name: 'Tom',
    quotedPrice: 2323,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 34,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: false,
    processState: 1,
    index: 4,
  },
  {
    name: 'Tom',
    quotedPrice: 723,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 265,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: false,
    processState: 3,
    index: 5,
  },
  {
    name: 'Tom',
    quotedPrice: 623,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 67,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: true,
    processState: 1,
    index: 6,
  },
  {
    name: 'Tom',
    quotedPrice: 523,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 15,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: false,
    processState: 2,
    index: 7,
  },
  {
    name: 'Tom',
    quotedPrice: 323,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 27,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: true,
    processState: 1,
    index: 8,
  },
  {
    name: 'Tom',
    quotedPrice: 823,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 47,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: false,
    processState: 2,
    index: 9,
  },
  {
    name: 'Tom',
    quotedPrice: 423,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 87,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: true,
    processState: 3,
    index: 10,
  },
  {
    name: 'Tom',
    quotedPrice: 263,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 85,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: false,
    processState: 1,
    index: 11,
  },
  {
    name: 'Tom',
    quotedPrice: 231,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 277,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: true,
    processState: 2,
    index: 12,
  },
  {
    name: 'Tom',
    quotedPrice: 243,
    address: 'No. 189, Grove St, Los Angeles',
    regionProject: '上海市普陀区',
    subscriptionRatio: 46,
    effectiveDate: '2016-05-03',
    img: 'https://dony.online/static/logo.png',
    initiateMode: false,
    processState: 3,
    index: 13,
  }
];

const searchParams = ref({
  ruleName: '',
  describe: '',
  name: '',
  categoryType: '',
  time: '',
});

const total = ref(1);
const pageNum = ref(1);
const pageSize = ref(10);
const selectedItems = ref(0);
const processStateList = ref([
  {
    color: 'rgba(0, 122, 245, 1)',
    text: '处理中',
    value: 1,
  },
  {
    color: 'rgba(245, 63, 63, 1)',
    text: '已完成',
    value: 2,
  },
  {
    color: 'rgba(16, 166, 103, 1)',
    text: '处理后',
    value: 3,
  }
]);
const tableData = ref<TableDataItem[]>([]);
const multipleTableRef = ref();
const multipleSelection = ref<TableDataItem[]>([]);
const handleExport = (rows?: TableDataItem[]) => {
  if (rows) {
    rows.forEach((row) => {
      multipleTableRef?.value?.toggleRowSelection(row, undefined);
    });
  } else {
    multipleTableRef?.value?.clearSelection();
  }
};

const handleOperation = () => {
  ElMessage.success('业务操作');
};

const handleBatchOperation = () => {
  ElMessage.success('批量操作');
};

const handleAdd = () => {
  ElMessage.success('新增');
};

const handleSelectionChange = (val: TableDataItem[]) => {
  multipleSelection.value = val;
  selectedItems.value = val.length;
};

const handleEdit = () => {
  ElMessage.success('编辑');
};

const handleDelete = () => {
  ElMessage.success('删除');
};

const initLoadData = (firstPage = false) => {
  if (firstPage) {
    pageNum.value = 1;
  }
  const start = (pageNum.value - 1) * pageSize.value;
  const end = pageNum.value * pageSize.value;
  tableData.value = tableDataTemp.slice(start, end);
  total.value = tableDataTemp.length;
};

const pageChangeHandle = (params: {currentPage: number, pageSize: number}) => {
  pageNum.value = params.currentPage;
  pageSize.value = params.pageSize;

  initLoadData();
};

const handleSearch = () => {
  initLoadData();
};

const getProcessStateText = (processState: number) => {
  const matchedState = processStateList.value.find((item) => item.value === processState);
  return matchedState ? matchedState.text : '';
};

const getProcessStateColor = (processState: number) => {
  const matchedState = processStateList.value.find((item) => item.value === processState);
  return matchedState ? matchedState.color : 'blue';
};

onMounted(() => {
  initLoadData(true);
});

</script>
<style lang="scss" scoped>
  .base-table {
    box-sizing: border-box;
    padding: 12px;
    background: #fff;
  }

  .picture {
    width: 100px;
    height: 40px;
  }

  .status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
  }

  .select-number {
    padding-left: 10px;
    font-size: 12px;
    font-weight: 400;

    .num {
      padding: 0 4px;
      color: rgb(0 122 245 / 100%);
    }
  }
</style>
